<template>
	<view class="categroy-container">
		<scroll-view scroll-y="true" class="scroll-view-left">
			<view v-for="(item,index) in leftMenuList" 
				:key="index" 
				:class="{'left-view-item':true,'active':activeIndex==index?true:false}" 
				@click="changeCategory(index)">{{item.title}}</view>
		</scroll-view>
		<scroll-view scroll-y="true" class="scroll-view-right" @scroll="changeScroll" :scroll-top="scrollTop">
			<view v-for="(item,index) in secondMenuList" :key="index" class="right-item">
				<u-sticky style="top:0">
					<view class="right-item-title">
						<text>{{item.title}}</text>
					</view>
				</u-sticky>
				<view class="right-item-main">
					<view class="item" v-for="(pro,idx) in item.goods" :key="idx" @click="goDetail(pro.gid)">
						<view class="product-image-box">
							<image :src="`http:${pro.image}`" mode=""></image>
						</view>
						<view class="product-title">
							<text>{{pro.title}}</text>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				leftMenuList:[],
				secondMenuList:[],
				activeIndex:0,
				scrollTop:0,
				timer:null
			}
		},
		methods: {
			async getLeftMenu(){
				const result=await uni.$api.home.getLeftMenusApi();
				this.leftMenuList=result.data.data;
				this.changeCategory(0)
			},
			async changeCategory(index){
				//将scrollTop的值设置为0
				this.scrollTop=0
				this.activeIndex=index;
				const cid=this.leftMenuList[index].cid
				const result=await uni.$api.home.getProductByRightCategoryApi(cid)
				console.log('result',result.data.data);
				this.secondMenuList=result.data.data;
			},
			changeScroll(e){
				if(this.timer){
					clearTimeout(this.timer)
				}
				this.timer=setTimeout(()=>{
					this.scrollTop=e.target.scrollTop
				},500)
				
			},
			goDetail(gid){
				uni.navigateTo({
					url:`/pages/productdetail/productdetail?gid=${gid}`
				})
			}
		},
		onLoad() {
			this.getLeftMenu()
		}
		
	}
</script>

<style lang="scss">
	.categroy-container{
		 display: flex;
		 height: 100vh;
		 font-size: 28rpx;
		.scroll-view-left{
			width: 190rpx;
			height: 100vh;
			background-color: #f5f5f5;
			.left-view-item{
				@include flex-box(center,center,row)
				height: 80rpx;
			}
			.active{
				position: relative;
				background-color: #fff;
				padding-left: 10rpx;
				&::before{
					position:absolute;
					display: 'block';
					content:' ';
					width: 5rpx;
					height: 60rpx;
					background-color: #f00;
					left: 0;
				}
			}
		}
		.scroll-view-right{
			flex-grow: 1;
			height: 100vh;
			background-color: #f5f5f5;
			.right-item{
				@include flex-box(evenly,start,column)
				.right-item-title{
					display: flex;
					width: 600rpx;
					height: 50rpx;
					padding: 5rpx;
					margin-left: 20rpx;
					align-items: center;
					background-color: #fff;
				}
				.right-item-main{
					@include flex-box(between,start,row)
					flex-wrap: wrap;
					padding: 20rpx;
					.item{
						@include flex-box(center,center,column)
						box-sizing: border-box;
						width: 180rpx;
						height: 250rpx;
						background-color: #fff;
						margin: 5rpx;
						border-radius: 10rpx;
						.product-image-box{
							image{
								width: 150rpx;
								height: 150rpx;
							}
						}
						.product-title{
							text{
								@include flex-ellipsis(1)
							}
						}
					}
					
				}
			}
		}
	}
</style>
